package com.mall.goods.service.impl;

import com.mall.goods.model.Product;
import com.mall.goods.model.Sku;
import com.mall.goods.model.Spu;
import com.mall.goods.mapper.SpuMapper;
import com.mall.goods.service.ISkuService;
import com.mall.goods.service.ISpuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

/**
 * <p>
 * 商品SPU表 服务实现类
 * </p>
 *
 * @author Darren
 * @since 2022-08-18
 */
@Service
public class SpuServiceImpl extends ServiceImpl<SpuMapper, Spu> implements ISpuService {
    @Autowired
    private ISkuService skuService;

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean addSpu(Spu spu){
        return super.save(spu);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean deleteById(String id){
        return super.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean updateSpu(Spu spu){
        return super.updateById(spu);
    }

    @Override
    public Spu getById(String id){
        return super.getById(id);
    }

    @Override
    public List<Spu> selectList(Spu spu){
        return super.list(getQueryWrapper(spu));
    }

    @Override
    public Page<Spu> selectPage(Page<Spu> page, Spu spu){
        return super.page(page, getQueryWrapper(spu));
    }

    @Override
    public Product findBySpuId(String spuId) {
        Spu spu = this.getById(spuId);
        List<Sku> skuList = skuService.list(new LambdaQueryWrapper<Sku>().eq(Sku::getSpuId, spuId));
        Product product = new Product();
        product.setSpu(spu);
        product.setSkuList(skuList);
        return product;
    }

    public LambdaQueryWrapper<Spu> getQueryWrapper(Spu spu){
        return Wrappers.<Spu>lambdaQuery()
            .eq(ObjectUtils.isNotEmpty(spu.getId()), Spu::getId, spu.getId())
            .eq(ObjectUtils.isNotEmpty(spu.getName()), Spu::getName, spu.getName())
            .eq(ObjectUtils.isNotEmpty(spu.getIntro()), Spu::getIntro, spu.getIntro())
            .eq(ObjectUtils.isNotEmpty(spu.getBrandId()), Spu::getBrandId, spu.getBrandId())
            .eq(ObjectUtils.isNotEmpty(spu.getCategoryOneId()), Spu::getCategoryOneId, spu.getCategoryOneId())
            .eq(ObjectUtils.isNotEmpty(spu.getCategoryTwoId()), Spu::getCategoryTwoId, spu.getCategoryTwoId())
            .eq(ObjectUtils.isNotEmpty(spu.getCategoryThreeId()), Spu::getCategoryThreeId, spu.getCategoryThreeId())
            .eq(ObjectUtils.isNotEmpty(spu.getImages()), Spu::getImages, spu.getImages())
            .eq(ObjectUtils.isNotEmpty(spu.getAfterSalesService()), Spu::getAfterSalesService, spu.getAfterSalesService())
            .eq(ObjectUtils.isNotEmpty(spu.getContent()), Spu::getContent, spu.getContent())
            .eq(ObjectUtils.isNotEmpty(spu.getAttributeList()), Spu::getAttributeList, spu.getAttributeList())
            .eq(ObjectUtils.isNotEmpty(spu.getIsMarketable()), Spu::getIsMarketable, spu.getIsMarketable())
            .eq(ObjectUtils.isNotEmpty(spu.getIsDelete()), Spu::getIsDelete, spu.getIsDelete())
            .eq(ObjectUtils.isNotEmpty(spu.getStatus()), Spu::getStatus, spu.getStatus())
        ;
    }
}
